package com.shengwei.truck.driver.view.adapter.mine;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;
import com.shengwei.truck.driver.R;
import com.shengwei.truck.driver.bean.MessageItem;
import com.shengwei.truck.driver.databinding.MsgItemBinding;
import java.util.List;

/**
 * 任务列表适配器
 */
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> {
    private LayoutInflater mInflater;
    private List<MessageItem> mDataList;
    private Context mContext;

    public MessageAdapter(Context context,List<MessageItem> msgList) {
        mInflater = LayoutInflater.from(context);
        mDataList = msgList;
        mContext = context;
    }

    public void notifyDataSetChanged(List<MessageItem> dataList) {
        this.mDataList = dataList;
        super.notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MsgItemBinding  msgItemBinding = DataBindingUtil.inflate(mInflater,R.layout.msg_item,parent,false);
        return new ViewHolder(msgItemBinding);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        MsgItemBinding viewDataBinding = (MsgItemBinding) holder.viewDataBinding;
        MessageItem msgItem = mDataList.get(position);

        if(msgItem.isRead == 1) {  //已读
            viewDataBinding.tvUnReadDot.setVisibility(View.INVISIBLE);
        }
        else{  //未读
            viewDataBinding.tvUnReadDot.setVisibility(View.VISIBLE);
        }
        viewDataBinding.setMsgItem(msgItem);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ViewDataBinding viewDataBinding;
        public ViewHolder(ViewDataBinding viewDataBinding) {
            super(viewDataBinding.getRoot());
            this.viewDataBinding = viewDataBinding;
        }
    }
}
